// pages/auth/accountLogin/accountLogin.js
var api =require('../../../config/api')
var util =require('../../../utils/util')
var app=getApp()
Page({

    data: {
        username:'',
        password:'',
    },
    accountLogin(){
        var that=this;
        if(this.data.password.length<1||this.data.username.length<0){
            wx.showModal({
                title:'错误信息',
                content:'请输入用户名和密码',
                showCancel:false
            })
            return false
        }
        wx.request({
            url: api.AuthLoginByAccount,
            data: {
                username: that.data.username,
                password: that.data.password
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                if (res.data.errno == 0) {
                    app.globalData.hasLogin = true;
                    wx.setStorageSync('userInfo', res.data.data.userInfo);
                    wx.setStorage({
                        key: "token",
                        data: res.data.data.token,
                        success: function() {
                            wx.switchTab({
                                url: '/pages/user/index/index'
                            });
                        }
                    });
                } else {
                    app.globalData.hasLogin = false;
                    util.showErrorToast('账户登录失败');
                }
            }
        });
    },
    bindUsernameInput(e){
        this.setData({
            username:e.detail.value
        })
    },
    bindPasswordInput(e){
        this.setData({
            password:e.detail.value
        })
    },
    clearInput(e){
        switch (e.currentTarget.id) {
          case 'clear-username':
              this.setData({
                  username:''
              });
              break;
              case 'clear-password':
              this.setData({
                  password:''
              });
              break;

        }
    },

    onLoad(options) {

    },

})